package com.hxxy.wyp.petsLover.common.rest.base.backend;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.hxxy.wyp.petsLover.RestBase;
import com.hxxy.wyp.petsLover.common.entities.News;
import com.hxxy.wyp.petsLover.common.entities.User;
import com.hxxy.wyp.petsLover.common.entities.vo.req.SearchObj;
import com.hxxy.wyp.petsLover.common.services.base.backend.NewsService;
import com.hxxy.wyp.petsLover.config.RedisConfig;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName NewsRest
 * @Description TODO
 * @Author wyp
 * @Date 2023/2/4 15:45
 * @Version 1.0
 */
@RestController
@ResponseBody
@RequestMapping("/news")
public class NewsRest extends RestBase {


    @Autowired
    private NewsService newsService;

    @Autowired
    private RedisConfig redisConfig;


    @RequestMapping(value="/all",method = RequestMethod.POST)

    public Object selectAllNews(@RequestBody SearchObj searchObj) {
        Gson gson = new Gson();
        long page = Long.parseLong(searchObj.getPage());
        long pageSize = Long.parseLong(searchObj.getPageSize());
        String o = (String) redisConfig.getRedisTemplateByDb(0).opsForValue().get("newsList:" + page + ":" + pageSize);
        if (StringUtils.isNoneBlank(o)) {
            Page page1 = gson.fromJson(o, Page.class);
            return ajaxSuccess(page1, "200", "从缓存种查询");
        }

        //构造分页构造器
        Page<News> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<News> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(News::getNewsViews);
        newsService.pageSearch(pageInfo, lambdaQueryWrapper);
        List<News> records = pageInfo.getRecords();
        records.stream().map((record) -> {
            List<String> collect = Arrays.stream(record.getNewsPic().split(",")).collect(Collectors.toList());
            record.setNewsPic(collect.get(0));
            record.setNewsPicVo(collect);
            return record;
        }).collect(Collectors.toList());
        String s = gson.toJson(pageInfo, Page.class);
        redisConfig.getRedisTemplateByDb(0).opsForValue().set("newsList:" + page + ":" + pageSize, s);
        redisConfig.getRedisTemplateByDb(0).expire("newList:" + page, Duration.ofMinutes(2L));
        return ajaxSuccess(pageInfo, "200", "查询成功！");
    }

    /**
     * 查询细纹详细信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/infoMore")
    public Object infoMore(@RequestParam("id") String id) {
        Gson gson = new Gson();
        Map<String, Object> map=new HashMap<String, Object>();
        String o = (String) redisConfig.getRedisTemplateByDb(0).opsForValue().get("newsInfo:" + id);
        if (StringUtils.isNoneBlank(o)) {
            News news = gson.fromJson(o, News.class);
            return ajaxSuccess(news, "200", "从缓存种查询成功!");
        }
        News news = newsService.getById(id);
        boolean aNull = Objects.isNull(news);
        List<String> picListVo = Arrays.stream(news.getNewsPic().split(",")).collect(Collectors.toList());
        news.setNewsPic(picListVo.get(0));
        news.setNewsPicVo(picListVo);
        Long newsViews = news.getNewsViews();
        news.setNewsViews(++newsViews);
        newsService.updateById(news);
        String s = gson.toJson(news, News.class);
        redisConfig.getRedisTemplateByDb(0).opsForValue().set("newsInfo:" + id, s);
        redisConfig.getRedisTemplateByDb(0).expire("newsInfo:" + id, Duration.ofMinutes(1L));
        return !aNull ? ajaxSuccess(news, "200", "查询成功!") : ajaxSuccess("200", "无此详情信息");
    }
}
